package com.java.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.dao.PlatformMapper;
import com.java.model.IndexHelp;
import com.java.model.IndexSlide;
import com.java.model.Platform;
import com.java.model.Result;
import com.java.service.PlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static java.lang.Integer.parseInt;

@Service
public class PlatformServiceImpl implements PlatformService {

    @Autowired
    private PlatformMapper platformMapper;

    @Override
    public Result save(Platform platform) {
        platformMapper.save(platform);
        return Result.success();
    }

    @Override
    public Result modify(Platform platform) {
        platformMapper.modify(platform);
        return Result.success();
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            platformMapper.remove(id);
        }
        return Result.success();
    }

    @Override
    public Result list(Platform platform) {
        PageHelper.startPage(platform.getPageNum(), platform.getPageSize()); // 提醒page在显示哪一页的数据和要显示多少条数据
        List<Platform> list = platformMapper.list(platform);
        PageInfo<Platform> pageInfo = new PageInfo<>(list);

        return Result.success(pageInfo.getList(), pageInfo.getTotal());
    }


    @Override
    public Result get(int id) {
        Platform platform = platformMapper.get(id);
        return Result.success(platform, null);
    }

    @Override
    public Result platformStatus(int[] ids, String status) {
        for (Integer id : ids) {
            Platform platform = new Platform();
            platform.setId(id);
            platform.setStatus(parseInt(status));
            platformMapper.platformStatus(platform);
        }
        return Result.success();
    }


    @Override
    public List<Platform> toList() {
        return platformMapper.toList();
    }
}
